package com.ajie.slimming.ui.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ajie.slimming.R;
import com.ajie.slimming.config.Constants;
import com.ajie.slimming.image.SmartImageView;
import com.ajie.slimming.ui.MainActivity;
import com.ajie.slimming.ui.SlimmingApplication;
import com.ajie.slimming.ui.UserInfoHelper;

public class LeftSlidingMenuFragment extends Fragment implements
        OnClickListener
{
    /** 个人中心 */
    private TextView user_info;
    
    /** 成绩 */
    private TextView result;
    
    /** 控制 */
    private TextView control;
    
    private TextView ageTextView, sexTextView, heightTextView;
    
    private SmartImageView left_smart;
    
    private SharedPreferences mSharedPreferences;
    
    private OnSharedPreferenceChangeListener preferenceListener = new OnSharedPreferenceChangeListener()
    {
        @Override
        public void onSharedPreferenceChanged(
                SharedPreferences sharedPreferences, String key)
        {
            refreshData();
        }
    };;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mSharedPreferences = SlimmingApplication.getBPContext()
                .getSharedPreferences(Constants.PRE_USER_INFO,
                        Context.MODE_PRIVATE);
        mSharedPreferences.registerOnSharedPreferenceChangeListener(preferenceListener);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.main_left_fragment,
                container,
                false);
        // left_smart = (SmartImageView) view.findViewById(R.id.left_smart);
        // left_smart.setImageUrl(null, R.drawable.logo, R.drawable.logo);
        ageTextView = (TextView) view.findViewById(R.id.age);
        sexTextView = (TextView) view.findViewById(R.id.sex);
        heightTextView = (TextView) view.findViewById(R.id.height);
        user_info = (TextView) view.findViewById(R.id.user_info);
        result = (TextView) view.findViewById(R.id.result);
        control = (TextView) view.findViewById(R.id.control);
        
        user_info.setOnClickListener(this);
        result.setOnClickListener(this);
        control.setOnClickListener(this);
        refreshData();
        return view;
    }
    
    private void refreshData()
    {
        String sex, height, weight, age;
        sex = mSharedPreferences.getString(UserInfoHelper.SEX, "");
        height = mSharedPreferences.getString(UserInfoHelper.HEIGHT, "");
        weight = mSharedPreferences.getString(UserInfoHelper.WEIGHT, "");
        age = mSharedPreferences.getString(UserInfoHelper.AGE, "");
        ageTextView.setText(age + "岁");
        sexTextView.setText(sex);
        heightTextView.setText(height + "cm  " + weight + "kg");
    }
    
    Fragment oldContent = new ResultFragment();
    
    @Override
    public void onClick(View v)
    {
        Fragment newContent = null;
        switch (v.getId())
        {
            case R.id.user_info: // 个人中心
                newContent = new UserInfoFragment();
                break;
            case R.id.result: // 成绩
                newContent = new ResultFragment();
                break;
            case R.id.control: // 控制
                newContent = new ControlFragment();
                break;
            default:
                break;
        }
        if (newContent != null)
        {
            switchFragment(newContent);
            oldContent = newContent;
        }
    }
    
    /*
     * 切换到不同的功能内容
     */
    private void switchFragment(Fragment fragment)
    {
        if (getActivity() == null)
            return;
        MainActivity ra = (MainActivity) getActivity();
        if (oldContent != null
                && fragment.getClass()
                        .getSimpleName()
                        .equals(oldContent.getClass().getSimpleName()))
        {
            ra.switchContent();
        }
        else
        {
            ra.switchContent(fragment);
            oldContent = fragment;
        }
    }
}
